package com.sk.rpcAssistNetty.io.netty;

/**
 * description：自定义一个Future，使用【等待-通知机制】实现线程间通讯
 *
 * @author 孙文斌
 * @Date: 2021/3/29 14:41
 */
public class DefaultFuture {
    private RpcResponse rpcResponse;
    private volatile boolean isSucceed = false;
    private final Object object = new Object();
    public RpcResponse getResponse(int timeout){
        synchronized (object){
            while (!isSucceed){
                try {
                    //wait
                        object.wait(timeout);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return rpcResponse;
        }
    }

    public void setResponse(RpcResponse response){
        if(isSucceed){
            return;
        }
        synchronized (object) {
            this.rpcResponse = response;
            this.isSucceed = true;
            //notiy
            object.notify();
        }
    }
}